
package com.v1.video.option.db;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.v1.video.headline.zerodelivery.ZeroVideoProvider;

public class SearchKeyDbUtil {

    public static Uri URI_SEARCH_KEY = ZeroVideoProvider.buildUri(SearchKeyInfo.class);
//    public static Uri URI_TIMING = ZeroVideoProvider.buildUri(ZeroTimingInfo.class);

    public static boolean addSearchKeyInfo(Context context, SearchKeyInfo keyInfo) {
        try {
            int result = updateTime(context, keyInfo);
            if (result == 0) {
                ContentResolver mResolver = context.getContentResolver();
                if(mResolver == null){
                	return false;
                }
                Uri uri = mResolver.insert(URI_SEARCH_KEY, keyInfo.beanToValues());
                if(uri == null){
                	return false;
                }                
                // 通过返回的uri可以获取到当条数据插入的行号，插入失败为-1
                String path = uri.getLastPathSegment();
                if (path.equals("-1")) {
//                    throw new Exception("insert error");
                	return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return false;
    }

    public static int deleteSearchKey(Context context, SearchKeyInfo keyInfo) {
        ContentResolver mResolver = context.getContentResolver();
        int result = mResolver.delete(URI_SEARCH_KEY, "key=?", new String[] {
                keyInfo.key
        });
        return result;
    }

    public static int deleteSearchKey(Context context, String key) {
        ContentResolver mResolver = context.getContentResolver();
        int result = mResolver.delete(URI_SEARCH_KEY, "key=?", new String[] {
                key
        });
        return result;
    }

    public static int updateTime(Context context, SearchKeyInfo keyInfo) {
        ContentResolver mResolver = context.getContentResolver();
        if(mResolver == null){
        	return -1;
        }
        int result = mResolver.update(URI_SEARCH_KEY, keyInfo.beanToValues(), "key=?",
                new String[] {
            keyInfo.key
                });
        return result;
    }

    /** 数据库中是否有数据 */
    public static boolean isHasData(Context context) {
        ArrayList<SearchKeyInfo> beanList = getKeyInfo(context);
        if (beanList.size() >= 1) {
            return true;
        } else {
            return false;
        }

    }

    public static ArrayList<SearchKeyInfo> getKeyInfo(Context context) {
        ContentResolver mResolver = context.getContentResolver();
        if(mResolver == null){
        	return null;
        }
        Cursor cursor = mResolver.query(URI_SEARCH_KEY, null, null, null, null);
        ArrayList<SearchKeyInfo> beanList = new ArrayList<SearchKeyInfo>();        
        if(cursor != null){
	        while (cursor.moveToNext()) {
	            SearchKeyInfo bean = new SearchKeyInfo();
	            bean.cursorToBean(cursor);
	            beanList.add(bean);
	        }
        }
        if(cursor != null){
    		cursor.close();
    	}
    	cursor = null;
        return beanList;
    }
}
